package com.example.zg.androidnewview;

import android.content.Context;
import android.content.res.Resources;
import android.graphics.Color;
import android.graphics.drawable.Drawable;
import android.support.design.widget.CoordinatorLayout;
import android.support.design.widget.Snackbar;
import android.support.v4.content.ContextCompat;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.Gravity;
import android.view.View;
import android.view.WindowManager;
import android.view.inputmethod.InputMethodManager;
import android.widget.TextView;

public class SnackbarActivity extends AppCompatActivity {
    private static final String TAG = "SnackbarActivity";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_snackbar);

    }

    public void showSnackbar(View view) {
        hideInputMethod();
        Snackbar snackbar = Snackbar.make(view, "message内容", Snackbar.LENGTH_LONG);
        //改变action按钮颜色
        snackbar.setActionTextColor(Color.RED);
        //获取到snackbar显示内容的控件
        View snackbarView = snackbar.getView();
        //改变背景色
        snackbarView.setBackgroundColor(Color.argb(100,250,0,250));
        //显示消息的控件
        TextView tv = (TextView) snackbarView.findViewById(R.id.snackbar_text);
        //改变消息文字颜色
        tv.setTextColor(Color.BLUE);
        //添加图标
        Drawable drawable = ContextCompat.getDrawable(this,R.drawable.img_audio);
        drawable.setBounds(0,0,50,50);
        tv.setCompoundDrawables(drawable,null,null,null);
        tv.setGravity(Gravity.CENTER);


        snackbar.setAction("点击", new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Log.i(TAG, "onClick: 点击了");

            }
        });

        snackbar.show();

    }


    private void hideInputMethod(){
        View view1 = getWindow().peekDecorView();
        if(view1!=null){
            InputMethodManager methodManager = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
            assert methodManager != null;
            methodManager.hideSoftInputFromWindow(view1.getWindowToken(),0);
        }
    }

    public void showSnackbar2(View view) {
        Snackbar snackbar = Snackbar.make(getWindow().getDecorView(), "message内容", Snackbar.LENGTH_LONG);
       



        snackbar.show();

    }
}
